﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicalManagementSystem.Models;
using MedicalManagementSystem.Utils;
using MedicalManagementSystem.Helpers;

namespace MedicalManagementSystem.Controllers
{
    public class DiagnosController : GenericController
    {
        //
        // GET: /Diagnos/
        [EnumAuthorize(Roles = UserRoles.DoctorRole | UserRoles.ArchiverRole 
                             | UserRoles.StatisticRole )]
        public ActionResult Index(int id)
        {
            DbContext.Instance.Diagnos.Each(diagnos => diagnos.Load());
            return View(DbContext.Instance.Diagnos.Where(diagnos => diagnos.DiagnosBloc.id == id));
        }

        //
        // GET: /Diagnos/Details/5
        [EnumAuthorize(Roles = UserRoles.DoctorRole | UserRoles.ArchiverRole
                             | UserRoles.StatisticRole)]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Diagnos/Create
        [EnumAuthorize(Roles = UserRoles.DoctorRole)]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Diagnos/Create
        [EnumAuthorize(Roles = UserRoles.DoctorRole)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "id")]Diagnos diagnos, int? DiagnosBlocId)
        {
            diagnos.DiagnosBloc = DiagnosBloc.GetByPK(DiagnosBlocId != null ? DiagnosBlocId.Value : 0);
            if (diagnos.CheckBeforeSave())
            {
                DbContext.Instance.AddToDiagnos(diagnos);
                DbContext.Instance.SaveChanges();

                return RedirectToAction("Index");
            }
            else
            {
                AddViolataionsToModelState(diagnos.GetRuleViolations());
            }

            return View(diagnos);
        }

        //
        // GET: /Diagnos/Edit/5
        [EnumAuthorize(Roles = UserRoles.DoctorRole)]
        public ActionResult Edit(int id)
        {
            var diagnos = Diagnos.GetByPK(id);
            diagnos.Load();
            return View(diagnos);
        }

        //
        // POST: /Diagnos/Edit/5
        [EnumAuthorize(Roles = UserRoles.DoctorRole)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int? DiagnosBlocId, int id,[Bind] Diagnos diagnos)
        {
            if (diagnos.CheckBeforeSave())
            {
                    var original = Diagnos.GetByPK(id);
                    original.DiagnosBloc = DiagnosBlocId != null ? DiagnosBloc.GetByPK(DiagnosBlocId.Value) : null;

                    DbContext.Instance.ApplyPropertyChanges(original.EntityKey.EntitySetName, diagnos);
                    DbContext.Instance.SaveChanges();
                    return RedirectToAction("Index");
            }
            AddViolataionsToModelState(diagnos.GetRuleViolations());
            return View(diagnos);
        }

        //
        // POST: /Diagnos/Delete/5
        [EnumAuthorize(Roles = UserRoles.DoctorRole)]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(int id)
        {
            try
            {
                var diagnos = Diagnos.GetByPK(id);
                DbContext.Instance.DeleteObject(diagnos);
                DbContext.Instance.SaveChanges(true);
            }
            catch (Exception e)
            {

            }

            return RedirectToAction("Index");
        }
    }
}
